import 'reflect-metadata';
import { ElMessage } from 'element-plus';
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';
import { ModuleFactory } from './decorators/factory';
import { AxiosRequestConfig, AxiosResponse } from 'axios';
import { injectViews } from './decorators/connect/vue3';
import { RouteRecordRaw } from 'vue-router';
import 'element-plus/dist/index.css';

class Application {
    async init() {
        this.rejesterModuleConfig();
        await this.rejesterRouter();
        this.mountApp();
    }

    rejesterModuleConfig() {
        ModuleFactory.configure({
            globalPrefix: '',
            showMessage: {
                success: (message: string) => ElMessage.success({ message }),
                fail: (message: string) => ElMessage.error({ message }),
            },
            interceptors: {
                response: (response: AxiosResponse) => {
                    const { data } = response;
                    if (data && data.code === 10000) {
                        return data.data; // 直接返回最终结果
                    }
                    return Promise.reject(response);
                },
                request: (config: AxiosRequestConfig) => {
                    return config;
                },
            },
        });
    }

    async rejesterRouter() {
        const routerConfig: RouteRecordRaw[] = await injectViews(require.context('./views/', true, /.vue/));
        routerConfig.forEach((route) => router.addRoute(route));
    }

    mountApp() {
        createApp(App).use(store).use(router).mount('#app');
    }
}

new Application().init();
